WPF笔记汇总之消息框与界面控件 您所在的位置:网站首页 wpf 聊天对话 WPF笔记汇总之消息框与界面控件

WPF笔记汇总之消息框与界面控件

2024-07-03 04:22| 来源: 网络整理| 查看: 265

WPF消息框与界面控件

接上一篇 《WPF笔记汇总之命令的使用》,这篇主要汇总WPF中的消息框及通用界面控件如工具栏,菜单栏,状态栏等控件的使用方法汇总。

文章目录 WPF消息框与界面控件1. 消息框的使用2. 对话框使用2.1 打开文件2.2 保存文件2.3 自定义对话框 3. 通用界面控件3.1 菜单控件3.2 上下文菜单3.3 工具栏控件3.4 状态栏控件

1. 消息框的使用

消息框就是提示用的,可以带标题,带图标,带多种选择的确定按钮,也可以返回用户的选择,返回的就是MessageBoxResult枚举中的值。

Simple MessageBox MessageBox with title MessageBox with buttons MessageBox with response MessageBox with icon MessageBox with default choice private void btnSimpleMessageBox_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hello, world!"); } private void btnMessageBoxWithTitle_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hello, world!", "My App"); } private void btnMessageBoxWithButtons_Click(object sender, RoutedEventArgs e) { MessageBox.Show("This MessageBox has extra options.\n\nHello, world?", "My App", MessageBoxButton.YesNoCancel); } private void btnMessageBoxWithResponse_Click(object sender, RoutedEventArgs e) { MessageBoxResult result = MessageBox.Show("Would you like to greet the world with a \"Hello, world\"?", "My App", MessageBoxButton.YesNoCancel); switch (result) { case MessageBoxResult.Yes: MessageBox.Show("Hello to you too!", "My App"); break; case MessageBoxResult.No: MessageBox.Show("Oh well, too bad!", "My App"); break; case MessageBoxResult.Cancel: MessageBox.Show("Nevermind then...", "My App"); break; } } private void btnMessageBoxWithIcon_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hello, world!", "My App", MessageBoxButton.OK, MessageBoxImage.Information); } private void btnMessageBoxWithDefaultChoice_Click(object sender, RoutedEventArgs e) { MessageBox.Show("Hello, world?", "My App", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No); } 2. 对话框使用 2.1 打开文件 Open file OpenFileDialog openFileDialog = new OpenFileDialog(); //openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.Filter = "Image files (*.png;*.jpeg)|*.png;*.jpeg|All files (*.*)|*.*"; // openFileDialog.InitialDirectory = @"d:\"; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); if (openFileDialog.ShowDialog() == true) txtEditor.Text = File.ReadAllText(openFileDialog.FileName); OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Multiselect = true; openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (openFileDialog.ShowDialog() == true) { foreach (string filename in openFileDialog.FileNames) lbFiles.Items.Add(Path.GetFileName(filename)); } 2.2 保存文件 Save file private void btnSaveFile_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text file (*.txt)|*.txt|C# file (*.cs)|*.cs"; saveFileDialog.InitialDirectory = @"D:\temp\"; saveFileDialog.Title = "WPF学习"; if (saveFileDialog.ShowDialog() == true) File.WriteAllText(saveFileDialog.FileName, txtEditor.Text); } 2.3 自定义对话框

类似新建一个窗体,然后调用这个窗体就行了

Question: Answer _Ok _Cancel public partial class MyInputDialogs : Window { public MyInputDialogs(string question, string defaultAnswer = "") { InitializeComponent(); lblQuestion.Content = question; txtAnswer.Text = defaultAnswer; } private void btnDialogOk_Click(object sender, RoutedEventArgs e) { this.DialogResult = true; } private void Window_ContentRendered(object sender, EventArgs e) { txtAnswer.SelectAll(); txtAnswer.Focus(); } public string Answer { get { return txtAnswer.Text; } } }

使用自定义输入窗体

MyInputDialogs inputDialog = new MyInputDialogs("Please enter your name:", "John Doe"); if (inputDialog.ShowDialog() == true) lblName.Text = inputDialog.Answer; 3. 通用界面控件 3.1 菜单控件 3.2 上下文菜单

上下文菜单就是,右键某个控件或者窗体的时候,弹出的菜单。

还可以用后置代码设置菜单

private void Button_Click(object sender, RoutedEventArgs e) { ContextMenu cm = this.FindResource("cmButton") as ContextMenu; cm.PlacementTarget = sender as Button; cm.IsOpen = true; } 3.3 工具栏控件

工具栏简单的说就是一个条状容器面板上放置几个控件,其中有几个重要的概念:溢出,溢出就是没有足够的空间显示工具栏上的所有按钮,WPF会将它们放入一个菜单,该菜单能通过单击工具栏右侧的箭头来访问。附加属性ToolBar.OverflowMode就是用来设置溢出方式的。 默认值为IfNeeded,工具栏项将放在溢出菜单中。 您可以使用始终Always或从不来Never替代默认值,就是始终将项目放在溢出菜单中或阻止项目移动到溢出菜单。 还有一个就是工具栏的放置位置,工具栏也可以位于应用程序窗口的底部,甚至可以在两侧。 WPF工具栏支持所有这些功能。工具栏置地就是简单地将工具栏与面板底部对接,而不是顶部。垂直工具栏需要使用工具栏托盘的Orientation属性。

Paste Paste Font size: 10 12 14 16 3.4 状态栏控件

状态栏用于显示有关应用程序当前状态的各种信息,如光标位置,字数,任务进度等。

private void txtEditor_SelectionChanged(object sender, RoutedEventArgs e) { int row = txtEditor.GetLineIndexFromCharacterIndex(txtEditor.CaretIndex); int col = txtEditor.CaretIndex - txtEditor.GetCharacterIndexFromLineIndex(row); lblCursorPosition.Text = "Line " + (row + 1) + ", Char " + (col + 1); lblCursorPosition2.Text = "Line " + (row + 1) + ", Char " + (col + 1); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有